package ui 
{
	import assets.UIAsset;
	import com.dozen.mc.DUIBackground;
	import com.dozen.mc.DUIEditHouseBt;
	import com.dozen.mc.DUIFarmland;
	import com.dozen.mc.DUIFinishBt;
	import common.event.DUIEventer;
	import common.util.MapBoxUtil;
	import interfaces.IDUIHomeDemo;
	import ui.fl.DUIBackgroundEx;
	import ui.fl.DUIBuildingEx;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import vo.MapObjectVO;
	import vo.MOConfigVO;
	
	/**
	 * ...
	 * @author Surui
	 */
	public class UIFade extends Sprite implements IDUIHomeDemo
	{
		/** 地图对象 */
		public var m_map:UIMap = new UIMap();
		/** 建筑集合 */
		private var m_buildings:Array = [];
		
		/** 编辑按钮 */
		private var m_editHouseBt:DUIEditHouseBt = new DUIEditHouseBt();
		/** 完成按钮 */
		private var m_finishBt:DUIFinishBt = new DUIFinishBt();
		/** 是否编辑模式 */
		public var editMode:Boolean = false;
		
		private static var m_self:UIFade;
		public function UIFade() 
		{
			GameCamera.self.follow( m_map );
			initMap();
			//initBuildings();
		}
		
		public static function get self():UIFade {
			m_self = m_self ? m_self : new UIFade();
			return m_self;
		}
		/**
		 * 初始化地图
		 */
		private function initMap():void {
			this.addChild( m_map );
			m_map.x = 0;
			m_map.y = 0;
			
			this.addChild(m_finishBt);
			m_finishBt.addEventListener(MouseEvent.CLICK, onFinishBtClick);
			m_finishBt.x = 0;
			
			this.addChild(m_editHouseBt);
			m_editHouseBt.addEventListener(MouseEvent.CLICK, onEditHouseBtClick);
			m_editHouseBt.x = 0;
		}
		/**
		 * 进入装扮模式按钮监听处理函数
		 * @param	event
		 */
		private function onEditHouseBtClick( event:MouseEvent ):void {
			m_editHouseBt.visible = false;
			m_finishBt.visible = true;
			editMode = true;
			DUIEventer.dispatch(DUIEventer.EDIT_MODULE_START);
		}
		/**
		 * 装扮完成按钮监听处理函数
		 * @param	event
		 */
		private function onFinishBtClick( event:MouseEvent ):void {
			m_finishBt.visible = false;
			m_editHouseBt.visible = true;
			editMode = false;
			DUIEventer.dispatch(DUIEventer.EDIT_MODULE_SAVE_END);
			
		}

		/* INTERFACE interfaces.IDUIHomeDemo */
		
		public function initBg(bgId:int):void
		{
			var background:DUIBackgroundEx = new DUIBackgroundEx();
			var moConfigVO:MOConfigVO = UIAsset.getConfigVoByTypeId(bgId);
			background.setSkin(new com.dozen.mc.DUIBackground());
			background.mouseEnabled = false;
			this.m_map.addChild( background );
			var row:int = int(moConfigVO.mask.charAt(2)) + 1;
			var cos:int = int(moConfigVO.mask.charAt(3)) + 1;
			this.m_map.init(row, cos);
		}
		
		public function setMapObjects(mapObjects:Array):void
		{
			for each( var obj:Object in mapObjects ) {
				var mapObjectVO:MapObjectVO = MapObjectVO.toMVO(obj);
				//trace(mapObjectVO)
				var moConfigVO:MOConfigVO = UIAsset.getConfigVoByTypeId(mapObjectVO.typeId);
				
				var farmland:MovieClip = new moConfigVO.url();
				farmland.mouseEnabled = false;
				farmland.cacheAsBitmap = true;
				
				var building:DUIBuildingEx = new DUIBuildingEx();
				building.itemId = mapObjectVO.itemId;
				building.setSkin( farmland, mapObjectVO.rotation );
				m_buildings.push(building);
				this.m_map.addBuilding( building );
				
				var realPoint:Point = MapBoxUtil.getRealPoint(mapObjectVO.positionX, mapObjectVO.positionY);
				building.x = realPoint.x;
				building.y = realPoint.y;
			}
		}
		/**
		 * 移动建筑重叠检测回调函数
		 * @param	result
		 */
		public function move(result:Boolean):void
		{
			this.m_map.selectedBuilding.light = !result;
		}
		/**
		 * 放置建筑回调函数
		 * @param	result
		 */
		public function putMoTo(result:Boolean):void
		{
			if ( result ) {
				this.m_map.selectedBuilding = null;
			}
		}
		
		public function recycle(result:Boolean):void
		{
			
		}
		
		public function changeBG(result:Boolean):void
		{
			
		}
		
		public function switchPosition(result:Boolean):void
		{
			
		}
	}

}